<template>
  <div class="mod-prod-brand">
    <div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" size="small">
        <div class="input-row">
          <el-form-item prop="name" label="模型名称:" class="search-form-item">
            <el-input type="text" v-model="searchForm.name" placeholder="模型名称"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange(true)">{{$t('crud.searchBtn')}}</div>
            <div class="default-btn" @click="resetSearchForm('searchForm')">{{$t('product.reset')}}</div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          class="default-btn primary-btn"
          @click.stop="addOrUpdateHandle()"
        >{{$t('crud.addTitle')}}
        </div>
      </div>
      <div class="table-con spec-table">
        <el-table
          ref="specListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%">

          <el-table-column
            prop="name"
            label="模型名称">
            <template slot-scope="scope">
              <div class="table-cell-text">{{ scope.row.name }}</div>
            </template>
          </el-table-column>

          <el-table-column
            prop="img"
            label="图片"
          >
            <template slot-scope="scope">
              <div class="table-cell-image">
                <ImgShow :src="scope.row.img"/>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="isScan"
            label="是否支持扫描">
            <template slot-scope="scope">
              <span v-if="scope.row.isScan === 0" class="tag-text">否</span>
              <span v-else class="tag-text">是</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            fixed="right"
            :label="$t('publics.operating')"
            width="180"
          >
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div
                  class="default-btn text-btn"
                  @click="addOrUpdateHandle(scope.row.id)"
                >{{$t('crud.updateBtn')}}
                </div>
                <div
                  class="default-btn text-btn"
                  @click="deleteHandle(scope.row.id)"
                >{{$t('brand.delete')}}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './model-add-or-update'

  export default {
    data () {
      return {
        dataForm: {
          indexImg: ''
        },
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
        // 修改
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        searchForm: {
          status: ''
        }
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (page) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/platform/museumModel/page'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              this.theData
            )
          )
        }).then(({data}) => {
          this.dataList = data.records
          this.page.total = data.total
          this.dataListLoading = false
        })
      },

      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.brandId
        })
        this.$confirm(this.$i18n.t('remindPop.makeSure') + ' ' + `[${id ? this.$i18n.t('remindPop.delete') : this.$i18n.t('remindPop.batchDeletion')}]` + ' ' + this.$i18n.t('remindPop.operation') + '?', this.$i18n.t('remindPop.remind'), {
          confirmButtonText: this.$i18n.t('remindPop.confirm'),
          cancelButtonText: this.$i18n.t('remindPop.cancel'),
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/platform/museumModel'),
            method: 'delete',
            params: {
              brandId: id
            }
          }).then(({data}) => {
            this.page.total = this.page.total - ids.length
            this.$message({
              message: this.$i18n.t('remindPop.succeeded'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.refreshChange()
              }
            })
          })
        })
      },
      // 条件查询
      searchChange (newData = false) {
        this.page.currentPage = 1
        this.getDataList(this.page, newData)
      },
      // 刷新回调
      refreshChange () {
        this.getDataList(this.page)
      },
      // 多选变化
      selectionChange (val) {
        this.dataListSelections = val
      },

      /**
       * 重置表单
       * @param {String} formName 表单名称
       */
      resetSearchForm (formName) {
        this.$refs[formName].resetFields()
        this.getDataList(this.page, this.searchForm) //重置表单加载初始数据
      },

      handleSizeChange (val) {
        this.page.pageSize = val
        this.getDataList()
      },
      handleCurrentChange (val) {
        this.page.currentPage = val
        this.getDataList()
      }
    }
  }
</script>
